package assist.datastore.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;


public abstract class NHLCSVParser<T> extends ArrayList<T>
{   
    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(NHLCSVParser.class.getName());
    public int nextRecord = 1;
    public NHLCSVParser(File csvFile, Integer lineNum, int total) throws IOException
    { 
      logger.info("Starting NHLCSV Parser");
      BufferedReader br = new BufferedReader(new FileReader(csvFile));
      String line = br.readLine();
      int i = 1;
      
      System.err.println("Starting:" + i);
      while ((line = br.readLine()) != null)
      {          
          if (lineNum > i++)
          {
              continue;
          }
          if (lineNum + total < i)
          {
              break;
          }
          if (i % 1000 == 0)
          {
              System.err.println("i:" + i);
          }
          
          T object = parseObject(line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)", -1));
          /*
          if (object instanceof RosterPlayer)
          {
              RosterPlayer rp = (RosterPlayer)object;
              if (rp.getKey().equals("200821155NEW JERSEY DEVILS27"))
              {
                  System.err.println(rp.getKey());
                  System.err.println("i:" + i);
              }
          }*/
          if (object != null)
          {
              this.add(object);
          }
      }      
      br.close();
      nextRecord = i - 1;
      System.err.println("i:" + i);
    }    
    public NHLCSVParser(final File csvFile, Integer lineNum) throws IOException
    {
        this(csvFile, lineNum, 100);
    }
    public Integer getNextRecordNum()
    {
        return this.nextRecord;
    }

    protected abstract T parseObject(String[] tokens);          
}
